﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Configuration
{
    public partial class AddDesignation : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                PopulateDesignation();

        }

        private void PopulateDesignation()
        {
            try
            {
                DesignationManager manager = new DesignationManager();
                List<Designation> designations = new List<Designation>();
                designations = manager.GetAllDesignations();
                designationGridview.DataSource = designations;
                designationGridview.DataBind();

            }
            catch (Exception)
            {

                throw;
            }

        }

        protected void SaveDesignation(object sender, EventArgs e)
        {
            try
            {
                Designation aDesignation = new Designation();
                DesignationManager manager = new DesignationManager();
                aDesignation.Title = txtDesignationName.Value;
                aDesignation.Grade = txtGradeName.Value;
                if (manager.Save(aDesignation))
                {

                    ClearInputs();
                    msgLabel.Text = "Designation Saved Successfully";
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Font.Bold = true;

                }
                else
                {
                    msgLabel.Text = "An error occured When Saving designation";
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Font.Bold = true;
                }
                PopulateDesignation();

            }
            catch (Exception ex)
            {

                msgLabel.Text = "An error occured When Saving designation";
                msgLabel.ForeColor = Color.Red;
                msgLabel.Font.Bold = true;
            }


        }

        protected void ClearInputs()
        {
            txtDesignationName.Value = "";
            txtGradeName.Value = "";
        }
        protected void designationGridview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            designationGridview.EditIndex = -1;
            PopulateDesignation();

        }

        protected void designationGridview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                DesignationManager manager = new DesignationManager();
                Designation designation = new Designation();
                designation.Id = Convert.ToInt32(((Label)designationGridview.Rows[e.RowIndex].FindControl("lblId")).Text);
                if (manager.Delet(designation))
                {
                    msgLabel.Text = "Designation deleted successfully";
                    msgLabel.ForeColor = Color.Green;
                    PopulateDesignation();
                }
                else
                {
                    msgLabel.Text = "Sorry, Designation Not deleted";
                    msgLabel.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void designationGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                DesignationManager manager = new DesignationManager();
                Designation designation = new Designation();
                designation.Id = Convert.ToInt32(((Label)designationGridview.Rows[e.RowIndex].FindControl("lblId")).Text);
                designation.Title = ((TextBox)designationGridview.Rows[e.RowIndex].FindControl("txtDesignation")).Text;
                designation.Grade = ((TextBox)designationGridview.Rows[e.RowIndex].FindControl("txtGrade")).Text;
                if (manager.Edit(designation))
                {
                    msgLabel.Text = "Designation Updated Successfully";
                    msgLabel.ForeColor = Color.Green;
                }
                else
                {
                    msgLabel.Text = "Some error occured when Updating";
                    msgLabel.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void designationGridview_RowEditing(object sender, GridViewEditEventArgs e)
        {
            designationGridview.EditIndex = e.NewEditIndex;
            PopulateDesignation();
        }

        protected void designationGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            designationGridview.PageIndex = e.NewPageIndex;
            PopulateDesignation();
        }
    }
}